import os,pathlib
from context_menu import menus
import settings

download_list_path=os.environ.get('download_list',None)
if not download_list_path:
    raise Exception("Download list path not set!")


def add_path_to_file(file_path,path_list):
    with open(file_path,'a',encoding='utf8') as f:
        for file in path_list:
            f.write(file+'\n')

def add_file_to_download(file_names:list[str],params:list[str]):
    add_path_to_file(download_list_path,file_names)


def add_download_menu():
    menus.FastCommand('Add To Download List',type='FILES',python=add_file_to_download).compile()

def remove_download_menu():
    menus.removeMenu('Add To Download List',type='FILES')

# Video module
video_list_path=os.getenv('video_list',None)
if not video_list_path:
    raise Exception('Video List path not set!')

def add_dir_video(dir_names, params):
    valid_dirs=[]
    for dir in [pathlib.Path(path) for path in dir_names]:
        for v in dir.rglob('*.mp4'):
            if v:
                valid_dirs.append(str(dir))
                break
        else:
            print('No video found in ',str(dir))
    add_path_to_file(video_list_path,valid_dirs)

def add_video_menu():
    menus.FastCommand('Add To Video List',type='DIRECTORY',python=add_dir_video).compile()

def remove_download_menu():
    menus.removeMenu('Add To Video List',type='DIRECTORY')

if __name__ == '__main__':
    add_video_menu()